<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>promise3_4</title>
</head>

<body>
    <script>
        //异步
        {
            let p1 = new Promise(function (resolve, reject) {
                setTimeout(() => {
                    // resolve(1);
                    reject("error")
                }, 1000)
            })
            let p2 = new Promise(function (resolve, reject) {
                setTimeout(() => {
                    resolve(2);
                }, 1000)
            })
            async function a() {
                try {
                    let l1 = await p1;
                    let l2 = await p2;
                    console.log(l1 + l2)
                } catch (e) {
                    console.log(e);
                }
            };
            a();
        }

        //1.promise封装过程不是异步操作，promise.then()的回调函数才是异步操作
        //2.同样的异步任务，执行的先后顺序取决于哪个异步任务是先被分配的
        //3.异步任务再去细分可以分为宏任务(setInterval setTimeout ajax操作 事件处理程序)和微任务(then方法)，同步代码执行完成之后，永远是先情空微任务对列，再去清空宏任务对列
        //4.我们把同步代码块当做宏任务，每一个宏任务在执行完成之后，都要判断当前有没有可以执行的微任务，然后再去读取下一个宏任务
        {
            console.log(1);
            let p2 = new Promise(function (resolve, reject) {
                setTimeout(() => {
                    console.log(3);
                    resolve();
                }, 0)
            })
            let p1 = new Promise(function (resolve, reject) {
                setTimeout(() => {
                    console.log(2);
                    resolve();
                }, 0)
            })
            setTimeout(() => { 
                console.log(4);
            }, 0);
            console.log(5)
        }

    </script>
</body>

</html>